package com.wgc.vip.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wgc.entity.PageResult;
import com.wgc.entity.Result;
import com.wgc.vip.domain.Vip;
import com.wgc.vip.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 会员
 */
@RestController
@RequestMapping("/vipList")
public class VipListController {
    @Autowired
    private VipService vipService;
    /**
     * 查看全部会员
     * */
    @RequestMapping("/selectAll")
    public Result<List<Vip>> selectAll(){
        return new Result<>("查询成功",vipService.selectAll());
    }
    /**
     * 添加会员
     * */

    @RequestMapping("/insert")
    public Result<Integer> insert(Vip vip){
        Integer insert = vipService.insert(vip);
        if(insert == -2){
            return new Result<>("添加失败，用户名已存在");
        }
        return new Result<>("插入成功");
    }

    /**
     * 修改会员
     * */

    @RequestMapping("/update")
    public Result<Integer> update(Vip vip){
        return new Result<>("修改成功",vipService.update(vip));
    }
    /**
     * 删除会员
     */
    @RequestMapping("/delete")
    public Result<Integer> delete(@RequestParam Integer id){
        return new Result<>("删除成功",vipService.delete(id));
    }
    /**
     * 条件查询会员表
     * @param iVip
     * @return
     */
    @PostMapping("/selectList")
    public Result< List<Vip> > select(@RequestBody Vip iVip) {
        return new Result<>("查询成功", vipService.selectList(iVip));
    }
    /**
     * 分页搜索实现会员表
     * @param page 默认值1
     * @param size 默认值10
     * @return
     */
    @GetMapping(value = "/pageAll" )
    public Result pageAll(@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<Vip> userPage = vipService.pageAll(page, size);
        PageResult<Vip> pageResult = new PageResult<>(userPage.getTotal(), userPage.getRecords());
        return new Result<>("查询成功", pageResult);
    }
    /**
     * 分页条件搜索实现用户表
     * @param vip
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/pageList" )
    public Result pageList(@RequestBody Vip vip,@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<Vip> VipPage = vipService.pageList(vip, page, size);
        PageResult<Vip> pageResult = new PageResult<>(VipPage.getTotal(), VipPage.getRecords());
        return new Result<>("查询成功", pageResult);
    }
    /**
     * 启动、
     * 停用
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/status")
    public Result updateStatus(@RequestParam Long id,@RequestParam Integer status){
        vipService.status(id,status);
        return new Result("修改成功");
    }
    /**
     * 登录
     *
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Vip Vip) {
        Integer login = vipService.login(Vip.getVipName(), Vip.getPassword());
        if (login == -1) {
            return Result.fail("用户名或密码错误");
        } else if (login == -2) {
            return Result.fail("该用户不存在");
        } else if (login == -3) {
            return Result.fail("该用户已停用");
        }
        return new Result("登录成功");
    }

}
